package com.zeildx.glife;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.zeildx.glife.entity.Word;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link GetaddFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GetaddFragment extends Fragment {
    private Button buttonSubmit;
    private EditText editTextdate,editTextschedule;
    private getViewModel getViewModel;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public GetaddFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment GetaddFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static GetaddFragment newInstance(String param1, String param2) {
        GetaddFragment fragment = new GetaddFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_getadd, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentActivity activity = requireActivity();
        getViewModel = ViewModelProviders.of(activity).get(com.zeildx.glife.getViewModel.class);
        buttonSubmit = activity.findViewById(R.id.buttonSubmit);
        editTextdate = activity.findViewById(R.id.editTextdate);
        editTextschedule = activity.findViewById(R.id.editTextschedule);
        buttonSubmit.setEnabled(false);
        editTextdate.requestFocus();
        InputMethodManager inn = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        inn.showSoftInput(editTextdate,0);

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String date = editTextdate.getText().toString().trim();
                String schedule = editTextschedule.getText().toString().trim();
                buttonSubmit.setEnabled(!date.isEmpty()&&!schedule.isEmpty());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        };
        editTextdate.addTextChangedListener(textWatcher);
        editTextschedule.addTextChangedListener(textWatcher);
        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String date = editTextdate.getText().toString().trim();
                String schedule = editTextschedule.getText().toString().trim();
                Word word = new Word(date,schedule);
                getViewModel.insertWords(word);
                NavController navController = Navigation.findNavController(view);
                navController.navigateUp();
                InputMethodManager inn = (InputMethodManager) requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                inn.hideSoftInputFromWindow(view.getWindowToken(),0);
            }
        });
    }
}